package qingma

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"go.uber.org/zap"
	"google.golang.org/grpc/status"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	"qm_system_server/server/model/qingma"
	"qm_system_server/server/proto/grpc/cost"
	"qm_system_server/server/service"
	"strconv"
)

type QmCostApi struct {
}

var qmCostService = service.ServiceGroupApp.QingmaServiceGroup.QmCostService

// CreateQmCost 创建qmCost表
// @Tags QmCost
// @Summary 创建qmCost表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body qingma.QmCost true "创建qmCost表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /qmCost/createQmCost [post]
func (qmCostApi *QmCostApi) CreateQmCost(c *gin.Context) {
	type request struct {
		GameHash   string  `json:"game_hash"`
		PlatformId int64   `json:"platformId"`
		Name       string  `json:"name"`
		Cost       float64 `json:"cost"`
		Month      string  `json:"month"`
	}

	var qmCost request
	err := c.ShouldBindJSON(&qmCost)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	qmCost.GameHash = c.Request.Header.Get("game-hash")

	rpcClient := cost.NewCostServiceClient(global.OperateGrpc)

	createResp, err := rpcClient.Create(context.Background(), &cost.CreateRequest{
		Cost: &cost.Cost{
			GameHash:   qmCost.GameHash,
			PlatformId: qmCost.PlatformId,
			Name:       qmCost.Name,
			Cost:       qmCost.Cost,
			Month:      qmCost.Month,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(createResp.Id, c)

}

// DeleteQmCost 删除qmCost表
// @Tags QmCost
// @Summary 删除qmCost表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body qingma.QmCost true "删除qmCost表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /qmCost/deleteQmCost [delete]
func (qmCostApi *QmCostApi) DeleteQmCost(c *gin.Context) {
	ID := c.Query("ID")

	//转成int64
	id := cast.ToInt64(ID)

	grpcClient := cost.NewCostServiceClient(global.OperateGrpc)
	_, err := grpcClient.Delete(context.Background(), &cost.DeleteRequest{
		Id: id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteQmCostByIds 批量删除qmCost表
// @Tags QmCost
// @Summary 批量删除qmCost表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /qmCost/deleteQmCostByIds [delete]
func (qmCostApi *QmCostApi) DeleteQmCostByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	if err := qmCostService.DeleteQmCostByIds(IDs); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateQmCost 更新qmCost表
// @Tags QmCost
// @Summary 更新qmCost表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body qingma.QmCost true "更新qmCost表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /qmCost/updateQmCost [put]
func (qmCostApi *QmCostApi) UpdateQmCost(c *gin.Context) {
	var qmCost qingma.QmCost
	err := c.ShouldBindJSON(&qmCost)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	grpcClient := cost.NewCostServiceClient(global.OperateGrpc)
	_, err = grpcClient.Update(context.Background(), &cost.UpdateRequest{
		Cost: &cost.Cost{
			Id:    int64(qmCost.ID),
			Name:  qmCost.Name,
			Cost:  qmCost.Cost,
			Month: qmCost.Month,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithMessage("更新成功", c)
}

// FindQmCost 用id查询qmCost表
// @Tags QmCost
// @Summary 用id查询qmCost表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query qingma.QmCost true "用id查询qmCost表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /qmCost/findQmCost [get]
func (qmCostApi *QmCostApi) FindQmCost(c *gin.Context) {
	//ID := c.Query("ID")
	//
	//
	//response.OkWithData(platform, c)
}

// GetQmCostList 分页获取qmCost表列表
// @Tags QmCost
// @Summary 分页获取qmCost表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query qingmaReq.QmCostSearch true "分页获取qmCost表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /qmCost/getQmCostList [get]
func (qmCostApi *QmCostApi) GetQmCostList(c *gin.Context) {
	type request struct {
		GameHash   string
		PlatformId int64  `form:"platform_id"`
		Month      string `form:"month"`
		Flag       int64  `form:"flag"`
	}

	var req request
	var platformId string
	_ = c.ShouldBindQuery(&req)
	req.GameHash = c.Request.Header.Get("game-hash")
	if req.Flag == 0 {
		platformId = c.Request.Header.Get("platform-id")
		if platformId != "" {
			req.PlatformId, _ = strconv.ParseInt(platformId, 10, 64)
		}
	}

	grpcClient := cost.NewCostServiceClient(global.OperateGrpc)

	costs, err := grpcClient.List(context.Background(), &cost.ListRequest{
		GameHash: req.GameHash,
		Platform: req.PlatformId,
		Month:    req.Month,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(response.PageResult{
		List: costs.Cost,
	}, c)
}
